package org.dfl.plugin.editor.actions;

import java.util.List;

import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.editor.commands.CancelCommandRuntimeException;
import org.dfl.plugin.editor.commands.TransitionEditPropertiesCommand;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.eclipse.gef.EditPart;
import org.eclipse.ui.IWorkbenchPart;

/**
 * edit transition properties action
 * @author Piotr Wlodarczyk
 *
 */
public class TransitionEditAction extends org.eclipse.gef.ui.actions.SelectionAction {

	public static final String EDIT = "EditTransition"; //$NON-NLS-1$

	public TransitionEditAction(IWorkbenchPart part) {
		super(part);
		setText(Messages.TransitionEditAction_name);
		setId(EDIT);
		setToolTipText(Messages.TransitionEditAction_tooltip);
		setImageDescriptor(DflPlugin.getImageDescriptor("icons/eclipse/properties.gif")); //$NON-NLS-1$
		setHoverImageDescriptor(getImageDescriptor());
	}

	@SuppressWarnings("unchecked")
	protected boolean calculateEnabled() {
		if (getSelectedObjects().isEmpty())
			return false;
		List parts = getSelectedObjects();
		for (int i = 0; i < parts.size(); i++) {
			Object o = parts.get(i);
			if (!(o instanceof EditPart))return false;
			EditPart part = (EditPart) o;
			
			if (!(part.getModel() instanceof TransitionModelElement)) return false;
			return new TransitionEditPropertiesCommand((TransitionModelElement) part.getModel()).canExecute();
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public void run() {
		List editparts = getSelectedObjects();
		for (int j = 0; j < editparts.size(); j++) {
			EditPart part = (EditPart) editparts.get(j);
			TransitionModelElement tme = (TransitionModelElement) part.getModel();
			try{
				execute(new TransitionEditPropertiesCommand(tme));
			}catch(CancelCommandRuntimeException ee){}
		}
	}

}
